package com.kaishengit.demo.atm;

import java.util.ArrayList;
import java.util.List;

public class Bank {

    private static Bank bank;
    private List<Account> list = new ArrayList<>();

    private Bank(){}

    public static Bank getInstance(){
        if (bank==null){
            bank = new Bank();
        }
        return bank;
    }

    public List<Account> getAccounts(){
        return list;
    }

    //保存账户
    public boolean saveAccount(Account account){
        return list.add(account);
    }

    //根据用户名查找
    public Account findByName(String username){

        for (int i=0;i<list.size();i++){
            String name = list.get(i).getName();
            if (name.equals(username)){
                return list.get(i);
            }
        }
        return null;
    }

    //根据用户名和密码查找
    public Account findByNameAndPass(String username,String password){

        for (int i=0;i<list.size();i++){
            String name = list.get(i).getName();
            String pass = list.get(i).getPassword();
            if (name.equals(username)&&pass.equals(password)){
                return list.get(i);
            }
        }
        return null;
    }


}
